// .........................................................................
// Title: flow_dynamics.do
//
// Examines the time series properties of flows into and out of insurance
// companies and fixed income funds
// .........................................................................

* ----------------------------------------------
* Aggregate reserve flows for insurance industry
* ----------------------------------------------

* Merge reserve flows (life and health)
use "$tmp/life_aggregate_reserves_flows.dta", clear
mmerge date_q using "$tmp/health_aggregate_reserves_flows"
drop _merge

cap drop quarter year
destring life_new_reserves health_new_reserves, force replace
gen new_reserves = life_new_reserves + health_new_reserves
replace new_reserves = life_new_reserves if missing(health_new_reserves)
sort date_q
foreach var of varlist *_reserves {
    replace `var' = `var' / 1e6
}
tsset date_q
save "$tmp/aggregate_reserves_flows", replace

* Consolidate stock and flow measures, impute p&c flows from stocks
use "$tmp/aggregate_reserves_stocks", clear
mmerge date_q using "$tmp/aggregate_reserves_flows.dta", unmatched(m)
sort date_q
drop _merge
gen pc_new_reserves = pc_tot_reserves - l.pc_tot_reserves
replace new_reserves = new_reserves + pc_new_reserves if ~missing(pc_new_reserves)
gen new_pct_reserves = new_reserves / l.tot_reserves
keep date_q new_reserves new_pct_reserves
save "$tmp/aggregate_reserves_deltas", replace

* ---------------------------------------
* Flows time series for funds
* ---------------------------------------

* Aggregate flows time series
use "$output/hd_panels/panel_fundlevel_cp_global_Q.dta", clear
keep if mns_class == "B" & ~inlist(mns_subclass, "S", "A", "LS", "SF", "SV")
replace DomicileCountry = "EMU" if inlist(DomicileCountry, $eu1) | inlist(DomicileCountry, $eu2) | inlist(DomicileCountry, $eu3)
keep if inlist(DomicileCountry, "USA", "EMU", "CAN", "GBR") | inlist(DomicileCountry, "CHE", "AUS", "SWE", "DEN", "NOR")
gcollapse (sum) flow_usd flow_usd_bet mvusd, by(date_q)
sort date_q
tsset date_q
gen fund_pct_flow = flow_usd_bet / l.mvusd
drop if flow_usd_bet == 0
save "$tmp/fund_pct_bc_flows", replace

* ---------------------------------------
* Plot insurance and fund flows together
* ---------------------------------------

* GZ credit spread at quarterly frequency
use "$tmp/gz_m.dta", clear
gen date_q = qofd(dofm(date_m))
format %tq date_q
gsort date_q -date_m
by date_q: keep if _n == 1
drop date_m
save "$tmp/gz_index_q", replace

* Merge series
use "$tmp/aggregate_reserves_deltas", clear
mmerge date_q using "$tmp/fund_pct_bc_flows", unmatched(m)
mmerge date_q using "$tmp/gz_index_q", unmatched(m)

* Correlations with GZ spread
corr new_pct_reserves gz_pct_delta
corr fund_pct_flow gz_pct_delta

* Construct and output plot (Figure 5)
line new_pct_reserves fund_pct_flow date_q if ~missing(fund_pct_flow), xtitle("") yline(0) ///
    lcolor(blue red) lpattern(solid dash) ytitle("Sectoral Inflows (%)") ///
    ylab(-.06 "-6%" -.04 "-4%" -.02 "-2%" 0 "0%" .02 "2%" .04 "4%" .06 "6%" ) ///
    legend(label(1 "Insurer Reserves Growth") label(2 "Corporate Bond Fund Inflows")) ///
    text(-.045 221 "{&rho}(Insurer Flows, GZ) = 32%") ///
    text(-.055 221.4 "{&rho}(Fund Flows, GZ) = -44%")

graph export "$graphs/flow_properties.pdf", as(pdf) replace
